package com.situ.wms.controller;

import java.io.IOException;
import java.lang.reflect.Member;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.wms.entity.Manager;
import com.situ.wms.impl.UserServiceImpl;
import com.situ.wms.service.UserService;
import com.situ.wms.util.BaseController;
import com.situ.wms.util.DBUtil;
import com.situ.wms.vo.LayuiTable;
import com.situ.wms.vo.ResultVO;


@WebServlet("/users")
public class UserController extends BaseController {
    private static final long serialVersionUID = 1L;
    UserService service = new UserServiceImpl();


    public void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/users.jsp").forward(request, response);
    }

    public LayuiTable list(Integer page, Integer limit, String keyword) {
        List<Manager> list = service.select(page, limit, keyword);
        //造一个前台解析格式的vo对象
        LayuiTable vo = new LayuiTable();
        //set值
        vo.setCode(0);
        vo.setData(list);
        vo.setCount(service.count(keyword));//统计具体几条返回
        return vo;
    }

    //根据id删除一个员工
    public void delete(Integer id) {
        service.delete(id);
    }

    public ResultVO save(Manager emp) {
        ResultVO vo = new ResultVO();
        if (emp.getId() == null) {
            emp.setPassword(DBUtil.md5(emp.getPassword()));
            vo = service.add(emp);
            return vo;
        } else {
            vo = service.selectById(emp.getId());
            Manager m = new Manager();
            m = (Manager) vo.getData();

            if (vo.getCode() == 0) {

                if (!m.getPassword().equals( emp.getPassword())) {
                    emp.setPassword(DBUtil.md5(emp.getPassword()));
                    service.update(emp);
                } else {

                    service.update(emp);
                }

            }
            return vo;
        }
    }

    public ResultVO select(Integer id) {
        System.out.println(111);
        return service.selectById(id);
    }


//	//根据id查询员工信息
//	public void select(HttpServletRequest request,HttpServletResponse response) throws IOException {
//		List<Department> list=deptservice.selectAll();
//		LayuiSelectVO vo=new LayuiSelectVO();
//		vo.setCode(0); 
//		vo.setData(list);
//		//System.out.println(list);
//		vo.setCount(Long.valueOf(list.size()));
//		//调用了fastjson2工具把对象转为json字符串
//		String str=JSON.toJSONString(vo);
//		response.setContentType("application/json;charset=UTF-8");
//		//写回给浏览器
//		PrintWriter pw =response.getWriter();
//		pw.write(str);
//	}

}
